<?php

namespace JLPay\Services\Openmerch\Device;

use JLPay\Services\BaseService;
use JLPay\Models\Openmerch\Device\UnionPayCodeQueryRequest;
use JLPay\Models\Openmerch\Device\UnionPayCodeQueryResponse;
use JLPay\Models\Openmerch\Device\UnionPayCodeAddRequest;
use JLPay\Models\Openmerch\Device\UnionPayCodeAddResponse;
use JLPay\Models\Openmerch\Device\DeviceChangeRequest;
use JLPay\Models\Openmerch\Device\DeviceChangeResponse;
use JLPay\Models\Openmerch\Device\DeviceCancelRequest;
use JLPay\Models\Openmerch\Device\DeviceCancelResponse;
use JLPay\Models\Openmerch\Device\DeviceAddRequest;
use JLPay\Models\Openmerch\Device\DeviceAddResponse;
use JLPay\Models\Openmerch\Device\DeviceQueryRequest;
use JLPay\Models\Openmerch\Device\DeviceQueryResponse;

/**
 * 设备管理服务类
 */
class DeviceService extends BaseService
{
    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();
        
        if ($apiPath) {
            return $baseUrl . $apiPath;
        }
        
        // 直接使用Request类中定义的完整API路径
        $apiUrl = constant($requestClass . '::API_URL');
        
        if (!$apiUrl) {
            // 如果没有定义API_URL，使用默认路径
            return $baseUrl . '/open/merch/access/device';
        }
        
        // 直接返回完整路径
        return $baseUrl . $apiUrl;
    }

    /**
     * 终端查询
     *
     * @param DeviceQueryRequest $request
     * @return DeviceQueryResponse
     */
    public function deviceQuery(DeviceQueryRequest $request): DeviceQueryResponse
    {
        return $this->post($request, DeviceQueryResponse::class);
    }

    /**
     * 查询银联码
     *
     * @param UnionPayCodeQueryRequest $request
     * @return UnionPayCodeQueryResponse
     */
    public function queryUnionpayCode(UnionPayCodeQueryRequest $request): UnionPayCodeQueryResponse
    {
        return $this->post($request, UnionPayCodeQueryResponse::class);
    }

    /**
     * 添加银联码
     *
     * @param UnionPayCodeAddRequest $request
     * @return UnionPayCodeAddResponse
     */
    public function addUnionpayCode(UnionPayCodeAddRequest $request): UnionPayCodeAddResponse
    {
        return $this->post($request, UnionPayCodeAddResponse::class);
    }

    /**
     * 变更设备
     *
     * @param DeviceChangeRequest $request
     * @return DeviceChangeResponse
     */
    public function changeDevice(DeviceChangeRequest $request): DeviceChangeResponse
    {
        return $this->post($request, DeviceChangeResponse::class);
    }

    /**
     * 撤销设备
     *
     * @param DeviceCancelRequest $request
     * @return DeviceCancelResponse
     */
    public function cancelDevice(DeviceCancelRequest $request): DeviceCancelResponse
    {
        return $this->post($request, DeviceCancelResponse::class);
    }

    /**
     * 添加设备
     *
     * @param DeviceAddRequest $request
     * @return DeviceAddResponse
     */
    public function addDevice(DeviceAddRequest $request): DeviceAddResponse
    {
        return $this->post($request, DeviceAddResponse::class);
    }
} 